<template>
  <div>
    <div class="ser_div">
      <el-row>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 产品 </el-col>
            <el-col :span="16">
              <el-select
                v-model="ser.product_id"
                filterable
                @change="product_id_change"
                placeholder="请选择产品"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in productList"
                  :key="index"
                  :label="item.product_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>

        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 颜色 </el-col>
            <el-col :span="16">
              <el-select
                filterable
                v-model="ser.color_id"
                placeholder="请选择颜色"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in ColorList"
                  :key="index"
                  :label="item.color"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="4">
          <el-button @click="clolr_click" class="add_btn" icon="el-icon-search">
            搜索</el-button
          >
          <el-button
            @click="add_color"
            v-permission="'system:users:add'"
            icon="el-icon-plus"
            class="add_btn"
            >新增颜色</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table :data="dataList" stripe style="width: 100%" border>
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column  label="产品"> 
        <template slot-scope="scope">
            <span v-for="item in scope.row.product" :key="item.id">
              {{item.product_name}}
            </span>
        </template>
      </el-table-column>
      <el-table-column prop="color" label="颜色"> </el-table-column>
      <el-table-column prop="created_at" label="创建日期"> </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            @click="set_product(scope.row)"
            type="text"
            icon="el-icon-edit"
            >编辑</el-button
          >
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="是否删除这条数据？"
            @confirm="del_product(scope.row)"
          >
            <el-button slot="reference" type="text" icon="el-icon-delete">
              删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        @prev-click="prev"
        @next-click="next"
        :current-page.sync="pagination.page"
        :page-size="pagination.per_page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="title"
      :visible.sync="isProduct"
      width="30%"
      :close-on-click-modal="false"
      @close="close_dialog"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="关联产品" prop="product_ids">
          <el-select
            v-model="ruleForm.product_ids"
            filterable
            multiple
            placeholder="请选择产品"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in productList"
              :key="index"
              :label="item.product_name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="颜色" prop="color_names">
          <el-select
                filterable
                multiple
                allow-create
                v-model="ruleForm.color_names"
                placeholder="请选择颜色或输入"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in dataList"
                  :key="index"
                  :label="item.color"
                  :value="item.color"
                >
                </el-option>
              </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="submitadd('ruleForm')"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getColorList,
  createColor,
  delColor,
  getProductList,
  getProductColorList,
} from "@/api/product/index";
export default {
  data() {
    return {
      ser: {
        product_id: "",
        color: "",
      },
      title: "",
      isProduct: false,
      dataList: [],
      ruleForm: {
        product_ids: [],
        color_names: [],
        id: "",
      },
      rules: {
        product_ids:[
          {
            required: true,
            message: "请选择产品",
            trigger: "blur",
          },
        ],
        color_names: [
          {
            required: true,
            message: "请选择或输入颜色名称",
            trigger: "blur",
          },
        ],
      },
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      productList: [],
      ColorList: [],
    };
  },
  mounted() {
    this.getlist();
    this.getproduct();
  },
  methods: {
    //产品列表
    getproduct() {
      getProductList({ per_page: 1000 }).then((res) => {
        if (res.code == 200) {
          this.productList = res.data.data;
        }
      });
    },
    product_id_change(val) {
      this.ColorList = [];
      this.get_produccolor(val);
    },
    get_produccolor(id) {
      getProductColorList({ product_id: id }).then((res) => {
        if (res.code == 200) {
          this.ColorList = res.data;
        }
      });
    },
    close_dialog() {
      this.ruleForm = {
        color: "",
        id: "",
      };
    },
    del_product(row) {
      delColor({
        id: row.id,
        is_del: 1,
      }).then((res) => {
        if (res.code == 200) {
          this.isProduct = false;
          this.$message.success("提交成功");
          this.getlist();
        }
      });
    },
    getlist() {
      getColorList({ ...this.pagination, ...this.ser }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    set_product(row) {
      this.ruleForm.color = row.color;
      this.ruleForm.id = row.id;
      this.title = "颜色编辑";
      this.isProduct = true;
    },
    add_color() {
      this.title = "新增颜色";
      this.isProduct = true;
      this.$nextTick(function () {
        this.$refs.ruleForm.resetFields();
      });
    },
    submitadd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {  
          createColor(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.getlist();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
    clolr_click() {
      this.pagination = {
        per_page: 10,
        page: 1,
      };
      getColorList({ color: this.color, ...this.pagination }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
  },
};
</script>

<style></style>
